package com.sales.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sales.dao.InvoiceRegisterMapper;
import com.sales.dao.SalesSlipMapper;
import com.sales.service.InvoiceRegisterService;
import com.sales.service.SalesSlipService;
import org.comple.erp.pojo.sales.InvoiceRegister;
import org.comple.erp.pojo.sales.SalesSlip;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InvoiceRegisterServiceImpl implements InvoiceRegisterService {

    @Autowired
    private InvoiceRegisterMapper invoiceRegisterMapper;

    @Autowired
    private SalesSlipMapper salesSlipMapper;
    @Override
    public PageInfo<InvoiceRegister> salectAll(Map<String, Object> params) {
        PageHelper.startPage(params);
        List<InvoiceRegister> invoiceRegisters = invoiceRegisterMapper.selectAll(params);
        PageInfo<InvoiceRegister> pageInfo = new PageInfo<>(invoiceRegisters,5);
        return pageInfo;
    }

    @Override
    public Boolean deleteByPrimaryKey(String invoiceNumber) {
        List<InvoiceRegister> invoiceRegisterList = invoiceRegisterMapper.selectByInvoiceNumber(invoiceNumber);
        Map<String,Object> params = new HashMap<>();
        for(InvoiceRegister invoiceRegister : invoiceRegisterList){
            params.put("isReturn",invoiceRegister.getIsReturn());
            params.put("code",invoiceRegister.getSalesSlipCode());
            SalesSlip salesSlip = salesSlipMapper.selectByCode(params);

            BigDecimal cost = invoiceRegister.getCurrentInvoiceAmount();
            BigDecimal newNotAmount = salesSlip.getNotAmount().add(cost);
            salesSlip.setNotAmount(newNotAmount);
            BigDecimal newInvoicedAmount = salesSlip.getInvoicedAmount().subtract(cost);
            salesSlip.setInvoicedAmount(newInvoicedAmount);
            salesSlipMapper.updateByPrimaryKeySelective(salesSlip);
        }

        if( invoiceRegisterMapper.deleteByPrimaryKey(invoiceNumber) >0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean insertSelective(InvoiceRegister record) {
        String slipId = record.getSlipId();
        SalesSlip salesSlip = salesSlipMapper.selectByPrimaryKey(slipId);
        BigDecimal cost = record.getCurrentInvoiceAmount();
        BigDecimal newNotAmount = salesSlip.getNotAmount().subtract(cost);
        salesSlip.setNotAmount(newNotAmount);
        BigDecimal newInvoicedAmount = salesSlip.getInvoicedAmount().add(cost);
        salesSlip.setInvoicedAmount(newInvoicedAmount);
        salesSlipMapper.updateByPrimaryKeySelective(salesSlip);

        if( invoiceRegisterMapper.insertSelective(record) >0){
            return true;
        }
        return false;
    }

    @Override
    public List<InvoiceRegister> selectByInvoiceNumber(String invoiceNumber) {
        List<InvoiceRegister> invoiceRegisters = invoiceRegisterMapper.selectByInvoiceNumber(invoiceNumber);
        return invoiceRegisters;
    }

    @Override
    public int selectCount(String invoiceNumber) {
        int count = invoiceRegisterMapper.selectCount(invoiceNumber);
        return count;
    }
}
